/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.util;

import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.view.View;

import com.bankscene.screenfit.dict.ErrorDict;
import com.bankscene.screenfit.exception.ViewModeException;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by jiiiiiin on 6/14/16.
 */

public class ViewUtils {

    private final View layoutView;

    public ViewUtils(@NonNull View layoutView) {
        checkNotNull(layoutView, ErrorDict.ERROR_LAYOUTVIEW_ISNULL);
        this.layoutView = layoutView;
    }

    @NonNull
    private final View findViewById(@NonNull @IdRes int id) {
        View view = this.layoutView.findViewById(id);
        checkNotNull(view, ErrorDict.ERROR_COMPONENTVIEW_ISNULL);
        return view;
    }

    @NonNull
    public final <T> T findViewById(@NonNull @IdRes int id, Class<T> viewClazz) throws ViewModeException{
        View view = findViewById(id);
        if (viewClazz.isInstance(view)) {
            return (T) view;
        } else {
            throw new ViewModeException(ErrorDict.ERROR_COMPONENTIDCLAZZ_NOTMATH);
        }
    }

}
